package com.jsptpd.smartauth.modules.business.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jsptpd.smartauth.modules.business.dao.db1.CollectorMapper;
import com.jsptpd.smartauth.modules.business.model.Collector;
import com.jsptpd.smartauth.modules.business.service.ICollectorService;

import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CollectorServiceImpl extends ServiceImpl<CollectorMapper, Collector> implements ICollectorService {

    @Override
    public boolean updateData(Collector collector) {
        List<Collector> results = selectList(new EntityWrapper<Collector>()
                .where("user_id={0}",collector.getUserId())
                .andNew("device_id={0}",collector.getDeviceId())
                .andNew("app_id={0}",collector.getAppId()));
        if(results.size()>0){
            return update(collector,
                    new EntityWrapper<Collector>().where("user_id={0}",collector.getUserId()));
        }else{
            return insert(collector);
        }
    }

    @Override
    public boolean collect(Collector collector) {
        //信息采集
        try {
            return updateData(collector);
        }catch (Exception e){
            return false;
        }

    }

    @Override
    public Collector selectOne(Collector collector) {
        //信息采集
        try {
            List<Collector> results = selectList(new EntityWrapper<Collector>()
                    //.where("user_id={0}",collector.getUserId())
                    .where("device_id={0}",collector.getDeviceId())
                    .andNew("app_id={0}",collector.getAppId()));
            if(results.size()>0){
                return results.get(0);
            }
        }catch (Exception e){
            return null;
        }
        return null;
    }
}
